package com.showmaker.controller;

import com.showmaker.bean.OrderSetting;
import com.showmaker.constant.MessageConstant;
import com.showmaker.entity.Result;
import com.showmaker.service.OrderSettingService;
import com.showmaker.utils.POIUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @DubboReference(interfaceClass = OrderSettingService.class, version = "1.0")
    private OrderSettingService orderSettingService;

    /**
     * 文件上传，实现预约设置数据批量导入
     * @param excelFile Excel文件，包含有多个预约数据
     * @return 返回批量导入的结果
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            // 使用工具类解析表格数据
            List<String[]> list = POIUtils.readExcel(excelFile);
            // 将 List<String[]> 转换成 List<OrderSetting>
            List<OrderSetting> data = new ArrayList<>();
            for (String[] strings : list) {
                String orderData = strings[0];
                String number = strings[1];
                OrderSetting orderSetting = new OrderSetting(new Date(orderData), Integer.parseInt(number));
                data.add(orderSetting);
            }
            // 调用方法，批量添加预约信息
            orderSettingService.add(data);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    /**
     * 获取指定月份的所有预约信息
     * @param date 指定预约信息的月份
     * @return 返回整个月份组成的集合
     */
    @GetMapping("/getOrderSetting")
    public Result getOrderSetting(String date) {
        try {
            List<Map> list = orderSettingService.getOrderSetting(date);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 设置预约人数
     * @param orderSetting 预约人数的相关信息(预约人数、预约日期)
     * @return 返回设置的结果
     */
    @PostMapping("/setPreNumber")
    public Result setPreNumber(@RequestBody OrderSetting orderSetting) {
        // System.out.println(orderSetting);
        // System.out.println(orderSetting.getNumber());
        // System.out.println(orderSetting.getOrderDate());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        // System.out.println(format.format(orderSetting.getOrderDate()));

        HashMap<String, Object> map = new HashMap<>();
        map.put("number", orderSetting.getNumber());
        map.put("date", format.format(orderSetting.getOrderDate()));

        try {
            orderSettingService.setPreNumber(map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }

        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }
}
